﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using RockCompany.Common.Lib;
using RockCompany.Common.Constant;
using RockCompany.Master.Model;
using RockCompany.Master.Controller;
using RockCompany.Master.DTO;
using RockCompany.Master.Constant;
using RockCompany.Common.Forms;

namespace RockCompany.Master.View
{
    public partial class SI03B : ParentFrm
    {
        #region Properties
        private Translator trans;
        private UserAuthenticate uAuth;
        private SI03BFormModel frmModel;
        private SI03A newMatCheckFrm;
        private string sSelectMatCheckNo;
        private string sUserName;
        #endregion

        #region Constructor
        //Testing
        public SI03B()
        {
            InitializeComponent();
            initDefaultData();
        } 

        public SI03B(Translator trans, string sUname, Form caller):base(sUname, caller)
        {
            InitializeComponent();
            this.trans = trans;
            trans.controlTranslate(this);
            sUserName = sUname;
            Owner = caller;
            initDefaultData();
        } 
        #endregion

        #region Utility Methods
        private void initDefaultData()
        {
            frmModel = new SI03BFormModel();
            frmModel.Owner = this;            
            frmModel.translator = this.trans;
            uAuth = new UserAuthenticate();
            uAuth.setRights(this, sUserName);

            dateTimePicker_ToDate.Value = DateTime.Today;
            dateTimePicker_FromDate.Value = DateTime.Today.AddDays(-1);

            PHIEU_KN_HN_DTO temp = new PHIEU_KN_HN_DTO();
            List<System.Reflection.PropertyInfo> properties = new List<System.Reflection.PropertyInfo>();
            properties.Add(temp.GetType().GetProperty(PHIEU_KN_HN_DTO.N_MS_PHIEU_KN));
            properties.Add(temp.GetType().GetProperty(PHIEU_KN_HN_DTO.N_PHE_DUYET));
            properties.Add(temp.GetType().GetProperty(PHIEU_KN_HN_DTO.N_MS_DON_HANG));
            properties.Add(temp.GetType().GetProperty(PHIEU_KN_HN_DTO.N_MS_HOA_DON));
            properties.Add(temp.GetType().GetProperty(PHIEU_KN_HN_DTO.N_NGAY_GIAO_HANG));
            properties.Add(temp.GetType().GetProperty(PHIEU_KN_HN_DTO.N_NHA_CUNG_UNG));
            properties.Add(temp.GetType().GetProperty(PHIEU_KN_HN_DTO.N_CREATION_TIME));
            properties.Add(temp.GetType().GetProperty(PHIEU_KN_HN_DTO.N_CREATION_USER));
            frmModel.buildDataGrid(dataGridView_MatCheckList, properties, null);

            properties.Clear();

            PHIEU_KN_HN_CT_DTO temp1 = new PHIEU_KN_HN_CT_DTO();
            NGUYEN_VLIEU_VTU_DTO nvl = new NGUYEN_VLIEU_VTU_DTO();
            properties = new List<System.Reflection.PropertyInfo>();
            properties.Add(temp1.GetType().GetProperty(PHIEU_KN_HN_CT_DTO.N_MA_NVL_VTU));
            properties.Add(nvl.GetType().GetProperty(NGUYEN_VLIEU_VTU_DTO.N_TEN_NVL_VTU));
            properties.Add(nvl.GetType().GetProperty(NGUYEN_VLIEU_VTU_DTO.N_QUY_CACH));
            properties.Add(nvl.GetType().GetProperty(NGUYEN_VLIEU_VTU_DTO.N_DON_VI));
            properties.Add(temp1.GetType().GetProperty(PHIEU_KN_HN_CT_DTO.N_SL_DAT_MUA));
            properties.Add(temp1.GetType().GetProperty(PHIEU_KN_HN_CT_DTO.N_SL_GIAO_HANG));
            properties.Add(temp1.GetType().GetProperty(PHIEU_KN_HN_CT_DTO.N_SL_THUC_NHAN));
            properties.Add(temp1.GetType().GetProperty(PHIEU_KN_HN_CT_DTO.N_GHI_CHU));
            frmModel.buildDataGrid(dataGridView_MatCheckDetail, properties, null);

            SI03BFormModel.bindCombo(comboBox_Supplier, PHIEU_KN_HN_DTO.N_NHA_CUNG_UNG, PHIEU_KN_HN_DTO.N_NHA_CUNG_UNG, frmModel.supplierLOV());
            SI03BFormModel.bindCombo(comboBox_MatImportNo, PHIEU_KN_HN_DTO.N_MS_DON_HANG, PHIEU_KN_HN_DTO.N_MS_DON_HANG, frmModel.importOrderLOV()); 
        }

        private void loadMatCheckList()
        {
            string sSup = comboBox_Supplier.Text;
            string sOrder = comboBox_MatImportNo.Text;
            string sfrom = dateTimePicker_FromDate.Value.ToString("yyyyMMdd");
            string sto = dateTimePicker_ToDate.Value.ToString("yyyyMMdd");
            dataGridView_MatCheckList.DataSource = frmModel.searchMatsCheckList(sSup, sOrder, sfrom, sto);
        }

        private void loadMatCheckDetail(string matCheckNumber)
        {
            dataGridView_MatCheckDetail.DataSource = frmModel.searchMatsCheckDetail(matCheckNumber);
        }
        #endregion

        #region Event Methods
        private void button_SearchMatCheck_Click(object sender, EventArgs e)
        {
            loadMatCheckList();
        }

        private void dataGridView_MatCheckList_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            sSelectMatCheckNo = dataGridView_MatCheckList.CurrentRow.Cells[0].EditedFormattedValue.ToString();
            loadMatCheckDetail(sSelectMatCheckNo);
        }

        private void button_NewMatCheck_Click(object sender, EventArgs e)
        {
            this.Hide();
            newMatCheckFrm = new SI03A(trans, sUserName, this);
            newMatCheckFrm.initDefaultData();
            newMatCheckFrm.Show();
        }

        private void button_ModMatCheck_Click(object sender, EventArgs e)
        {
            if (InforDialog.showConfirmBox(trans.getMessageCaption(CommonConstantIF.CONFIRM_CAPTION), trans.getMessageCaption(CommonConstantIF.CONFIRM_MODIFY)))
            {
                if (dataGridView_MatCheckList.CurrentRow != null)
                {
                    this.Hide();
                    newMatCheckFrm = new SI03A(trans, sUserName, this);
                    if(!CommonModel.existData(frmModel.matCheckDetails))
                    {
                        sSelectMatCheckNo = dataGridView_MatCheckList.CurrentRow.Cells[0].EditedFormattedValue.ToString();
                        loadMatCheckDetail(sSelectMatCheckNo);
                    }
                    newMatCheckFrm.initExistData(dataGridView_MatCheckList.CurrentRow, frmModel.matCheckDetails);
                    newMatCheckFrm.initDefaultData();
                    newMatCheckFrm.Show();
                }
            }
        }

        private void button_DelMatCheck_Click(object sender, EventArgs e)
        {
            if (InforDialog.showConfirmBox(trans.getMessageCaption(CommonConstantIF.CONFIRM_CAPTION), trans.getMessageCaption(CommonConstantIF.CONFIRM_DELETE)))
            {
                bool b1 = frmModel.removeMatCheckDetail(sSelectMatCheckNo), b2 = frmModel.removeMatCheck(sSelectMatCheckNo);
                if (sSelectMatCheckNo != "" && (b1 || b2))
                {
                    InforDialog.showInforBox(Owner, trans.getMessageCaption(CommonConstantIF.RemoveComplete), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
                    loadMatCheckList();
                }
                else
                    InforDialog.showErrorBox(Owner, trans.getMessageCaption(CommonConstantIF.RemoveError), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
            }
        }

        private void button_PrintRP_Click(object sender, EventArgs e)
        {
            if (dataGridView_MatCheckList.CurrentRow != null)
            {
                int index = dataGridView_MatCheckList.CurrentRow.Index;
                PHIEU_KN_HN_DTO curOb = new PHIEU_KN_HN_DTO();
                curOb.MS_PHIEU_KN = frmModel.matCheck.Rows[index][PHIEU_KN_HN_DTO.N_MS_PHIEU_KN].ToString();
                curOb.NHA_CUNG_UNG = frmModel.matCheck.Rows[index][PHIEU_KN_HN_DTO.N_NHA_CUNG_UNG].ToString();
                curOb.MS_DON_HANG = frmModel.matCheck.Rows[index][PHIEU_KN_HN_DTO.N_MS_DON_HANG].ToString();
                curOb.MS_HOA_DON = frmModel.matCheck.Rows[index][PHIEU_KN_HN_DTO.N_MS_HOA_DON].ToString();
                curOb.NGAY_GIAO_HANG = DateTime.Parse(frmModel.matCheck.Rows[index][PHIEU_KN_HN_DTO.N_NGAY_GIAO_HANG].ToString());
                if (InforDialog.showConfirmBox(trans.getMessageCaption(CommonConstantIF.CONFIRM_CAPTION), trans.getMessageCaption(CommonConstantIF.CONFIRM_PRINT)))
                {

                    if (frmModel.export2Excel(InforDialog.showSaveFileDlg(this), curOb))
                    {
                        InforDialog.showInforBox(this, trans.getMessageCaption(CommonConstantIF.SaveComplete), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
                    }
                    else
                        InforDialog.showErrorBox(this, trans.getMessageCaption(CommonConstantIF.SaveError), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
                }
            }
        }

        private void button_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion        
    }
}
